//
//  THJSettingVC.m
//  THJXHW
//
//  Created by 河南德艺 on 17/4/15.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJSettingVC.h"
#import "THJRevisePswVC.h"
#import "JPNavigationController.h"
#import "THJLoginVC.h"
#import "ZBNetworking.h"

@interface THJSettingVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableview;
}
@end

@implementation THJSettingVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTablview];
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:@"设置"];
    }else{
        [self setNavBarWithTitle:@"设置"];
    }
    
    // Do any additional setup after loading the view from its nib.
}
-(void)setTablview{
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, NavbarHeight,ScreenWidth,ScreenHeight-NavbarHeight) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableview];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray * titleArr = @[@"修改密码",@"退出登录",@"清除缓存"];
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.imageView.image = kGetImage(titleArr[indexPath.row]);
    cell.textLabel.text = titleArr[indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0){
        THJRevisePswVC * vc = [[THJRevisePswVC alloc] init];
        UINavigationController * nvc = [[UINavigationController alloc] initWithRootViewController:vc];
        [self presentViewController:nvc animated:YES completion:nil];
    }else if(indexPath.row == 2){
        
        [SVProgressHUD showWithStatus:@"清除中"];
        [[ZBCacheManager sharedInstance]clearCache];
        [self performSelector:@selector(clearSuccess) withObject:nil afterDelay:1];
    }else{
        __weak typeof (self) weakself = self;
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"你确定退出登录吗" preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
          
            [weakself.navigationController popViewControllerAnimated:YES];
            weakself.tabBarController.selectedIndex = 0;
            NSString * deleteUrl = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=member&r=member&m=logout"];
            NSString * token = [THJTools getToken];
            [MBProgressHUD showMessage:@"" toView:self.view];
            [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
                request.urlString=deleteUrl;
                request.apiType=ZBRequestTypeCache;
                [request setValue:token forHeaderField:@"token"];
            }  success:^(id responseObject,apiType type){
                NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                [MBProgressHUD hideHUDForView:weakself.view animated:YES];
                NSLog(@"退出登录反馈-%@",receiveData);
            } failed:^(NSError *error){
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                NSLog(@"error-%@",error);
            }];
            [THJTools signOut];
            THJLoginVC * vc = [[THJLoginVC alloc] init];
            //退出登录发出通知
            [self.tabBarController presentViewController:vc animated:YES completion:nil]; 
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"signOut" object:@"signOut"];
        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }]];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}
-(void)clearSuccess{
    [SVProgressHUD showSuccessWithStatus:@"清除成功"];
    [SVProgressHUD dismissWithDelay:1];
}
@end
